package pri.damai.xiaowu.usercenter.sysauth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import pri.damai.xiaowu.usercenter.sysauth.repository.SysOrgDao;
import pri.damai.xiaowu.usercenter.sysauth.entity.SysOrg;
import pri.damai.xiaowu.usercenter.sysauth.service.SysOrgService;
import org.springframework.stereotype.Service;
import pri.damai.xiaowu.common.core.tree.TreeUtils;
import pri.damai.xiaowu.usercenter.vo.OrgVO;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (SysOrg)表服务实现类
 *
 * @author makejava
 * @since 2022-03-26 22:39:42
 */
@Service("sysOrgService")
public class SysOrgServiceImpl extends ServiceImpl<SysOrgDao, SysOrg> implements SysOrgService {

    public List<OrgVO> getOrgTree() {
        List<SysOrg> sysOrgList = this.list();

        List<OrgVO> orgList = sysOrgList.stream().map(sysOrg -> {
            OrgVO orgVO = new OrgVO();
            BeanUtils.copyProperties(sysOrg, orgVO);
            orgVO.setId(sysOrg.getId());
            orgVO.setParentId(Long.valueOf(sysOrg.getParentId()));
            return orgVO;
        }).collect(Collectors.toList());

        return TreeUtils.toTreeNonRecursion(orgList, TreeUtils.TOP_PARENT_ID);
    }

}

